<?php

/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace common\rest\grid;

use Closure;
use yii\base\BaseObject;
use yii\helpers\ArrayHelper;

/**
 * Column is the base class of all [[GridView]] column classes.
 *
 * For more details and usage information on Column, see the [guide article on data widgets](guide:output-data-widgets).
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Column extends BaseObject {

    /**
     * @var GridView the grid view object that owns this column.
     */
    public $grid;

    /**
     * @var string the header cell content. Note that it will not be HTML-encoded.
     */
    public $header;

    /**
     * @var string the footer cell content. Note that it will not be HTML-encoded.
     */
    public $footer;

    /**
     * @var callable This is a callable that will be used to generate the content of each cell.
     * The signature of the function should be the following: `function ($model, $key, $index, $column)`.
     * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
     * and `$column` is a reference to the [[Column]] object.
     */
    public $content;

    /**
     * @var bool whether this column is visible. Defaults to true.
     */
    public $visible = true;

    /**
     * @var bool whether this column is visible. Defaults to true.
     */
    public $callback = false;

    /**
     * @var array the HTML attributes for the column group tag.
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
     */
    public $options = [];

    /**
     * @var array the HTML attributes for the header cell tag.
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
     */
    public $headerOptions = [];

    /**
     * @var array|\Closure the HTML attributes for the data cell tag. This can either be an array of
     * attributes or an anonymous function ([[Closure]]) that returns such an array.
     * The signature of the function should be the following: `function ($model, $key, $index, $column)`.
     * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
     * and `$column` is a reference to the [[Column]] object.
     * A function may be used to assign different attributes to different rows based on the data in that row.
     *
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
     */
    public $contentOptions = [];

    /**
     * @var array the HTML attributes for the footer cell tag.
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
     */
    public $footerOptions = [];

    /**
     * @var array the HTML attributes for the filter cell tag.
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
     */
    public $filterOptions = [];

    /**
     * @var string the HTML display when the content of a cell is empty.
     * This property is used to render cells that have no defined content,
     * e.g. empty footer or filter cells.
     *
     * Note that this is not used by the [[DataColumn]] if a data item is `null`. In that case
     * the [[\yii\i18n\Formatter::nullDisplay|nullDisplay]] property of the [[formatter]] will
     * be used to indicate an empty data value.
     */
    public $emptyCell = '&nbsp;';

    /**
     * Renders the header cell.
     */
    public function renderClassName() {
        return ArrayHelper::getValue($this->headerOptions, 'class');
    }

    /**
     * Renders the header cell.
     */
    public function renderFormat() {
        if ($this->callback) {
            return null;
        }
        if (isset($this->format) && $this->format) {
            return [
                'type' => $this->format,
            ];
        }
        return null;
    }

    /**
     * Renders the header cell.
     */
    public function renderExtands() {
        return [];
    }

    /**
     * Renders the header cell.
     */
    public function renderOptions() {
        $options = ArrayHelper::merge($this->options, $this->renderExtands());
        return !empty($options) ? $options : null;
    }

    /**
     * Renders the header cell.
     */
    public function renderHeaderCell() {
        return [
            'label' => $this->renderHeaderCellContent(),
            'prop' => isset($this->attribute) ? $this->attribute : null,
            'className' => $this->renderClassName(),
            'format' => $this->renderFormat(),
            'callback' => $this->callback,
            'options' => $this->renderOptions(),
            'sortable' => $this->renderSortable(),
            'align' => ArrayHelper::getValue($this->headerOptions, 'align', 'left'),
            'width' => ArrayHelper::getValue($this->headerOptions, 'width'),
            'fixed' => ArrayHelper::getValue($this->headerOptions, 'fixed'),
        ];
    }

    /**
     * Renders the footer cell.
     */
    public function renderFooterCell() {
        return $this->renderFooterCellContent();
    }

    /**
     * Renders a data cell.
     * @param mixed $model the data model being rendered
     * @param mixed $key the key associated with the data model
     * @param int $index the zero-based index of the data item among the item array returned by [[GridView::dataProvider]].
     * @return string the rendering result
     */
    public function renderDataCell($model, $key, $index) {
        return $this->renderDataCellContent($model, $key, $index);
    }

    /**
     * Renders the filter cell.
     */
    public function renderFilterCell() {
        return $this->renderFilterCellContent();
    }

    /**
     * Renders the header cell content.
     * The default implementation simply renders [[header]].
     * This method may be overridden to customize the rendering of the header cell.
     * @return string the rendering result
     */
    protected function renderHeaderCellContent() {
        return trim($this->header) !== '' ? $this->header : $this->getHeaderCellLabel();
    }

    /**
     * Returns header cell label.
     * This method may be overridden to customize the label of the header cell.
     * @return string label
     * @since 2.0.8
     */
    protected function getHeaderCellLabel() {
        return $this->emptyCell;
    }

    /**
     * Renders the footer cell content.
     * The default implementation simply renders [[footer]].
     * This method may be overridden to customize the rendering of the footer cell.
     * @return string the rendering result
     */
    protected function renderFooterCellContent() {
        return trim($this->footer) !== '' ? $this->footer : $this->grid->emptyCell;
    }

    /**
     * Renders the data cell content.
     * @param mixed $model the data model
     * @param mixed $key the key associated with the data model
     * @param int $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
     * @return string the rendering result
     */
    protected function renderDataCellContent($model, $key, $index) {
        if ($this->content !== null) {
            return call_user_func($this->content, $model, $key, $index, $this);
        }

        return $this->emptyCell;
    }

    /**
     * Renders the filter cell content.
     * The default implementation simply renders a space.
     * This method may be overridden to customize the rendering of the filter cell (if any).
     * @return string the rendering result
     */
    protected function renderFilterCellContent() {
        return $this->emptyCell;
    }

}
